package com.liuzh.kaoyanspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzh.kaoyanspringboot.common.Constants;
import com.liuzh.kaoyanspringboot.entity.Article;
import com.liuzh.kaoyanspringboot.entity.Comment;
import com.liuzh.kaoyanspringboot.entity.Like;
import com.liuzh.kaoyanspringboot.entity.User;
import com.liuzh.kaoyanspringboot.mapper.LikeMapper;
import com.liuzh.kaoyanspringboot.service.ArticleService;
import com.liuzh.kaoyanspringboot.service.CommentService;
import com.liuzh.kaoyanspringboot.service.LikeService;
import com.liuzh.kaoyanspringboot.service.MessageService;
import com.liuzh.kaoyanspringboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-03-12
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;
    @Resource
    private LikeMapper likeMapper;
    @Autowired
    private MessageService messageService;

    @Override
    public Like selectUserIsLike(Like like) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",like.getUserId());
        queryWrapper.eq("type",like.getType());
        queryWrapper.eq("like_id",like.getLikeId());
        return getOne(queryWrapper);
    }

    @Override
    public boolean cancelLike(Like like) {
        String type = like.getType();
        if (type.equals("article")) {
            // 文章点赞数减一
            articleService.decrementLike(like.getLikeId());
        }
        if (type.equals("comment")) {
            // 评论点赞数减一
            commentService.decrementLike(like.getLikeId());
        }
//        if (type == "user")
        return updateById(like);
    }

    @Override
    public boolean addLike(Like like) {
        String type = like.getType();
        Integer likeId = like.getLikeId();
//        String username = TokenUtils.getCurrentUser().getUsername();
        if (type.equals("article")) {
            // 文章点赞数加一
            articleService.increaseLike(likeId);
//            Article article = articleService.findById(likeId);
//            messageService.createAndSave(article.getUserId(),"like",article.getId(),username +"给你的文章: "+article.getName() +"点赞");

        }
        if (type.equals("comment")) {
            // 评论点赞加一
            commentService.increaseLike(like.getLikeId());
//            Comment comment = commentService.getById(likeId);
//            messageService.createAndSave(comment.getUserId(),"like",comment.getId(),username +"给你的评论: "+comment.getContent() +"点赞");
        }
//        System.out.println(like);
//        if (type == "user")
        return updateById(like);
    }


    @Override
    public boolean saveLike(Like like) {
        String type = like.getType();
        Integer likeId = like.getLikeId();
        User currentUser = TokenUtils.getCurrentUser();
        String nickname = currentUser.getNickname();
        Integer userId = currentUser.getId();
        if (type.equals("article")) {
            // 文章点赞数加一
            articleService.increaseLike(likeId);
            Article article = articleService.findById(likeId);
            messageService.createAndSave(userId , article.getUserId(), Constants.TYPE_ARTICLE, article.getId(),nickname +"给你的文章: "+article.getName() +"点赞");

        }
        if (type.equals("comment")) {
            // 评论点赞加一
            commentService.increaseLike(like.getLikeId());
            Comment comment = commentService.getById(likeId);
            messageService.createAndSave(userId , comment.getUserId(), Constants.TYPE_COMMENT, comment.getId(),nickname +"给你的评论: "+comment.getContent() +"点赞");
        }
//        System.out.println(like);
//        if (type == "user")
        return save(like);
    }

    @Override
    public Integer myCount() {
        return likeMapper.myCount();
    }

}
